$(document).ready(function () {

    var title;
    var nick;
    var subTitle;
    var textAlign= "left";
    var fontColor= "orange";
    var headerImgUrl;
    var cropWidth;
    var cropHeight;
    var articleData;
    var articleId;//预览文章时id
    var isPreviewClick=false;
    var isSaveClick=false;
    
    var hasTitle=false;
    var hasSubTitle=false;
    var hasNick=false;
    var hasHeaderImg=false;

    var mainImgHtml = '<div type=1 class="mainImg innerContainer" name="正文图片"><span class="mainImgText">正文图片</span><span class="editContainer"><span class="edit" innerType="mainImg">编辑</span><span class="delete">删除</span><span></div>';
    var paragarphHtml = '<div type=0 class="paragraph innerContainer" name="正文段落"><span class="paragraphText">编辑正文段落</span><span class="editContainer"><span class="edit" innerType="paa">编辑</span><span class="delete">删除</span><span></div>';
    var smallTitleHtml = '<div type=6 class="smallTitle innerContainer"name="小标题"><span class="smallTitleText">小标题</span><span class="editContainer"><span class="edit" innerType="smallTitle">编辑</span><span class="delete">删除</span><span></div>';
    var videoHtml = '<div type=2 class="videoContainer innerContainer"name="视频"><span class="vedios"><span class="videoInner"></span><span class="editContainer"><span class="edit" innertype="editVideo">编辑</span><span class="delete">删除</span><span></div>';
    var goodsHtml = '<div type=3 class="goods innerContainer"name="商品">商品id:<span class="goodsIdText"> </span> <span class="goodsintrotitle" style="display:none">商品简介:</span><span class="goodsintroText" style="display:none"></span><span class="editContainer"><span class="edit" innerType="goood">编辑</span><span class="delete">删除</span><span></div>';
    var auctionGoodsHtml = '<div type=8 class="auct innerContainer"name="拍品">拍品id:<span class="auctionIdText"></span> <span class="auctionintrotitle" style="display:none">拍品简介:</span><span class="auctionintroText" style="display:none"></span><span class="editContainer"><span class="edit" innerType="auctionGood">编辑</span><span class="delete">删除</span><span></div>';
    var studioHtml = '<div type=7 class="studio innerContainer"name="工作室"><span class="studioText">工作室</span><span class="editContainer"><span class="edit" innerType="studio">编辑</span><span class="delete">删除</span><span></div>';
    var selectedWorksHtml = '<div type=4 id="selectedWorks" class="innerContainer" name="作品精选"><div class="item ctrl-title">作品精选</div><span class="worksIds"></span><ul><li><div class="selecctedImg"></div><div class="selectedInf"><p class="selectedName">商品名称</p><p class="selectedPrice">￥</p></div><span class="editContainer"><span class="edit" innerType="selectedWork">编辑</span><span class="delete">删除</span><span></li></ul></div><div class="clear"></div>';
    var articleRelatedHtml = '<div type=5 id="artileRelated" class="innerContainer" name="相关文章"><div class="item ctrl-title">相关文章</div><span class="articleIds"></span><ul><li><div class="article">相关文章</div></li></ul><span class="editContainer"><span class="edit" innerType="articleEdit">编辑</span><span class="delete">删除</span><span></div><div class="clear"></div>	';
    var linkHtml = '<div type=9 class="links innerContainer" name="超链接"><span class="linkText">编辑超链接</span><span class="linkUrlText"></span><span class="editContainer" ><span class="edit" innerType="linkWord">编辑</span><span class="delete">删除</span><span></div>';

    var mainImgEditHtml='<div name="正文图片"class="diy-ctrl-item"><div class="item"><input type="file"class="upFile"filetype="mainImgFile"></div><div class="item"><input type="text"class="mainImgDesc"placeholder="请输入图片注释"></div></div>';
    var articleHeaderEditHtml = '<div name="大标题"class="diy-ctrl-item"><div class="item">文章大标题:<input id="bigTitle" type="text"placeholder="请输入文章标题"/></div><div class="item">显示方式:<label><input name="bigTitleFont"type="radio"value="left" checked/>居左</label><label><input name="bigTitleFont"type="radio"value="center"/>居中</label></div><div class="item">文章作者:<input id="nick" type="text"placeholder="请输入文章作者"/></div></div>';
    var abstractEditHtml = '<div name="摘要"class="diy-ctrl-item"><div class="item">字体颜色:<input name="abstractFont"type="radio"value="orange" checked/><span class="circlr orange"></span><input name="abstractFont"type="radio"value="green"/><span class="circlr green"></span></div><textarea id="subTitle" placeholder="请输入摘要"></textarea></div>';
    var paragarphEditHtml = '<div name="正文段落"class="diy-ctrl-item"><div class="item ctrl-title">正文段落</div><textarea class="paragraphEdit" placeholder="请输入内容"></textarea></div>';
    var smallTitleEditHtml = '<div name="小标题"class="diy-ctrl-item"><div class="item ctrl-title">小标题</div><input class="smallTitleInput" type="text"placeholder="请输入小标题"/></div>';
    var videoEditHtml = '<div name="视频"class="diy-ctrl-item"><div class="item ctrl-title">视频上传</div><div class="itemTwo">视频上传:<input type="file" class="upFile" filetype="videoFile"></span></div><div>封面图上传:<input type="file" filetype="imgvideoFile" class="upFile "></div></div>';
    var goodsEditHtml = '<div class="diy-ctrl-item"><div class="item ctrl-title">商品</div><div class="item"><input type="radio" value="goodsTplOne" name="goodsTpl" chcked>样式1<input type="radio" value="goodsTplTwo" name="goodsTpl">样式2</div><div class="item">商品id:<input class="goodsInput" type="text"placeholder="请输入商品id"/></div><div class="goodsIntro" style="display:none">商品简介:<textarea type="text"class="goodsIntroInput" placeholder="请输入商品简介"></textarea></div></div>';
    var auctionEditHtml = '<div name="拍品"class="diy-ctrl-item"><div class="item ctrl-title">拍品</div><div class="item">样式:<input type="radio" value="auctionTplOne" name="auctionTpl" chcked>样式1<input type="radio" value="auctionTplTwo" name="auctionTpl">样式2</div><div class="item">拍品id:<input class="auctionInput" type="text"placeholder="请输入拍品id"/></div><div class="auctionIntro" style="display:none">拍品简介:<textarea class="auctionIntroInput" placeholder="请输入拍品简介"></textarea></div></div>';
    var studioEditHtml = '<div name="工作室"class="diy-ctrl-item"><div class="item ctrl-title">工作室</div><div class="item">工作室id:<input class="studioInput" type="text"placeholder="请输入工作室id"/></div></div>';
    var selectedWorksEditHtml = '<div name="作品精选"class="diy-ctrl-item"><div class="item ctrl-title">作品精选</div><div class="item">作品1 id:<input class="worksInput" type="text"placeholder="作品1 id"></div><div class="selectedBtn"><span class="btn selectedAdd">+添加</span></div><div style="margin-top:10px"><span class="btn save">保存</span></div></div>';
    var articleRelatedEditHtml = '<div name="相关文章"class="diy-ctrl-item"><div class="item ctrl-title">相关文章</div><div class="item">文章1 id:<input type="text" class="articleInput" placeholder="文章1 id"></div><div><span class="btn articleAdd">+添加</span></div><div style="margin-top:10px"><span class="btn save">保存</span></div></div>';
    var linkEditHtml = '<div name="链接"class="diy-ctrl-item"><div class="item ctrl-title">超链接</div><div class="item">链接文本:<input class="linksEdit" type="text" placeholder="请输入文本"></div><div>链接url:<input class="linksUrlEdit" type="text" placeholder="请输入链接"></div></div>';

    var topicId = $("#topicId").val();
    var requestUrl;
    var hasTopicID = false;

    var list = [];
    //alert($("#topicId").val());
 
    //编辑文章
    if (topicId != null && topicId != undefined && topicId != "") {

        hasTopicID = true;
        requestUrl = "/cms/post/article/showInfo?topicId=" + topicId;

        $.ajax({
            url: requestUrl,
            success: function (result) {

                $("#headerImg").find(".headerImgText").css("display", "none");
                $("#headerImg").prepend("<img src='" + result.data.postCover.imgUrl + "' width='100%'>");
                cropWidth = result.data.postCover.cropWidth;
                cropHeight = result.data.postCover.cropHeight;
                $("#articleTiitle").html(result.data.title);
                $("#author").html(result.data.nick);
                $(".abstractText").html(result.data.subTitle);
                
                title=result.data.title;
                nick=result.data.nick;
                subTitle=result.data.subTitle;
                
                hasTitle=true;
                hasSubTitle=true;
                hasHeaderImg=true;
                hasNick=true;

                textAlign=result.data.textAlign;
                fontColor=result.data.fontColor;

                list = result.data.medias;
                editModules();

            }
        })
    }

    //上传首图 正文图片 正文视频
    $("#articleEditContainer").on('change', '.upFile', function () {
        var obj = $(this);
        var indexs = parseInt(obj.attr("index"));
        var fileType = $(this).attr("filetype");
        
        //mainImgFile  正文图片
        
        //videoFile  正文视频
        //imgvideoFile 正文图片
        //             首图
        
        var formData = new FormData();
        formData.append('url', $(obj)[0].files[0]);
        formData.append('type', 1);
        $.ajax({
            url: '/cms/post/article/uploadMedia',
            type: 'POST',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "text",
            success: function (data) {

                var data = data.substring(1, data.length).split("**");
                var url = data[0];

                var imgCropWidth = data[1];
                var imgCropHeight = data[2];              
                
                if(fileType == "videoFile"){  //视频地址
                    var video = url;
                    list[indexs].video = video;
                }
                else if(fileType == "imgvideoFile"){ //视频图片  
                    list[indexs].videoImage = url;                   
                    list[indexs].cropWidth=imgCropWidth;
                    list[indexs].cropHeight=imgCropHeight;
                    list[indexs].enable=1;

                    editModules();
                }
                else if(fileType == "mainImgFile"){ //正文图片
                	   list[indexs].imgKey = url;
                	   list[indexs].cropWidth=imgCropWidth;
                     list[indexs].cropHeight=imgCropHeight;                     
                     list[indexs].enable=1;

                     editModules();
                }
                else{                      //上传首图
                	if ($("#headerImg").find("img").length > 0) {
                        $("#headerImg").find("img").attr("src", url);
                    }
                    else {
                        $("#headerImg").find(".headerImgText").css("display", "none");
                        $("#headerImg").prepend("<img src='" + url + "' width='100%'>");
                    }                   
                    cropWidth = imgCropWidth;
                    cropHeight = imgCropHeight;
                    hasHeaderImg=true;
                }               

            }
        })
    })

    //增加一个模块
    $(".j-page-addModule").click(function () {
        var type = parseInt($(this).attr("type"));
        var listItem = {};
        listItem.type = type;
        list.push(listItem);
        editModules();
    })

    //处理模块
    function editModules() {
        var modulsHtml = "";
        for (var i = 0; i < list.length; i++) {
            if (list[i].type == 0) {      //正文
                modulsHtml += paragarphHtml;
                list[i].warnMsg="段落未填写完整";
            }
            else if (list[i].type == 1) {  //图片
                modulsHtml += mainImgHtml;
                list[i].warnMsg="正文图片未上传完整";
            }
            else if (list[i].type == 2) {  //视频
                modulsHtml += videoHtml;
                list[i].warnMsg="视频未上传完整";
            }
            else if (list[i].type == 3) {  //商品
                modulsHtml += goodsHtml;
                list[i].warnMsg="商品未填写完整";
            }
            else if (list[i].type == 4) {  //精选作品
                modulsHtml += selectedWorksHtml;
                list[i].warnMsg="作品精选未填写完整";
            }
            else if (list[i].type == 5) {  //文章
                modulsHtml += articleRelatedHtml;
                list[i].warnMsg="相关文章未填写完整";
            }
            else if (list[i].type == 6) {   // 小标题
                modulsHtml += smallTitleHtml;
                list[i].warnMsg="小标题未填写完整";
            }
            else if (list[i].type == 7) { //工作室
                modulsHtml += studioHtml;
                list[i].warnMsg="工作室未填写完整";
            }
            else if (list[i].type == 8) { //拍品
                modulsHtml += auctionGoodsHtml;
                list[i].warnMsg="拍品未填写完整";
            }
            else if (list[i].type == 9) {  //链接
                modulsHtml += linkHtml;
                list[i].warnMsg="链接未填写完整";
            }
        }

        $(".diy-phone-editContainer").html(modulsHtml);

        for (var i = 0; i < list.length; i++) {
            $(".innerContainer").eq(i).attr("index", i);
            if (list[i].type == 0) {      //正文
                if(list[i].desc!=null&&list[i].desc!=undefined&&list[i].desc!=""){
                    list[i].enable=1;
                }
                $(".innerContainer").eq(i).find(".paragraphText").html(list[i].desc);
            }
            else if (list[i].type == 1) {  //图片
                if(list[i].imgKey!=null&&list[i].imgKey!=undefined&&list[i].imgKey!=""){
                    list[i].enable=1;
                    $(".innerContainer").eq(i).find(".mainImgText").before("<img src='" + list[i].imgKey + "' width='100%'>");
                }
                
                if(list[i].desc!=null&&list[i].desc!=undefined){
                	$(".innerContainer").eq(i).find(".mainImgText").html(list[i].desc);
                }
                             
            }
            else if (list[i].type == 2) {  //视频
                if(list[i].video!=null&&list[i].video!=undefined&&list[i].video!=""&&list[i].videoImage!=null&&list[i].videoImage!=undefined&&list[i].videoImage!=""){
                    list[i].enable=1;
                }
                $(".innerContainer").eq(i).find(".videoInner").html("<img src='" + list[i].videoImage + "' width='100%'>");
            }
            else if (list[i].type == 3) {  //商品
                if(list[i].bizId!=null&&list[i].bizId!=undefined&&list[i].bizId!=""){
                    list[i].enable=1;
                }
                $(".innerContainer").eq(i).find(".goodsIdText").html(list[i].bizId);
                if(parseInt(list[i].tpl)==2){
                	$(".innerContainer").eq(i).find(".goodsintrotitle").css("display","block");
                	$(".innerContainer").eq(i).find(".goodsintroText").css("display","block");
                	$(".innerContainer").eq(i).find(".goodsintroText").html(list[i].desc);
                }
            }
            else if (list[i].type == 4) {  //精选作品           	
                if(list[i].bizList!=null&&list[i].bizList!=undefined&&list[i].bizList!=""){
                	var worksIdhtml="";
                    list[i].enable=1;
                    for (var j = 0; j < list[i].bizList.length; j++) {
                        var num = j + 1;
                        worksIdhtml += '<span>作品' + num + ' id:' + list[i].bizList[j]+ '</span>';

                    }
                    $(".innerContainer").eq(i).find(".worksIds").html(worksIdhtml);
                }
            }
            else if (list[i].type == 5) {  //文章
 
                if(list[i].bizList!=null&&list[i].bizList!=undefined&&list[i].bizList!=""){
                	 list[i].enable=1;
                     var articleIdhtml = "";
                     for (var j = 0; j < list[i].bizList.length; j++) {
                    	 
                         var num = j + 1;
                         articleIdhtml += '<span>文章' + num + ' id:' + list[i].bizList[j]+ '</span>';

                     }
                    $(".innerContainer").eq(i).find(".articleIds").html(articleIdhtml);
                }
            }
            else if (list[i].type == 6) {   // 小标题
                if(list[i].desc!=null&&list[i].desc!=undefined&&list[i].desc!=""){
                    list[i].enable=1;
                }
                $(".innerContainer").eq(i).find(".smallTitleText").html(list[i].desc);
            }
            else if (list[i].type == 7) { //工作室
                if(list[i].bizId!=null&&list[i].bizId!=undefined&&list[i].bizId!=""){
                    list[i].enable=1;
                }
                $(".innerContainer").eq(i).find(".studioText").html("工作室id: " + list[i].bizId);
            }
            else if (list[i].type == 8) { //拍品
                if(list[i].bizId!=null&&list[i].bizId!=undefined&&list[i].bizId!=""){
                    list[i].enable=1;
                }
                $(".innerContainer").eq(i).find(".auctionIdText").html(list[i].bizId);
            }
            else if (list[i].type == 9) {  //链接
                if(list[i].title!=null&&list[i].title!=undefined&&list[i].title!=""&&list[i].desc!=null&&list[i].desc!=undefined&&list[i].desc!=""){
                    list[i].enable=1;
                }
                $(".innerContainer").eq(i).find(".linkText").html(list[i].title);
                $(".innerContainer").eq(i).find(".linkUrlText").html(" 链接地址: " + list[i].desc);
            }
        }


    }

    //删除某一项
    $(".diy-phone").on('click', '.delete', function () {
        $(".diy-cirl1").css("display", "none");
        var index = parseInt($(this).parents(".innerContainer").attr("index"));
        list.splice(index, 1);
        var r = confirm("你确定要删除这一项吗?");
        if (r == true) {
            editModules();
        }

    });

    //编辑某一项
    $(".diy-phone").on('click', '.edit', function () {
        var index = parseInt($(this).parents(".innerContainer").attr("index"));
        var _this = $(this);
        var _thisParent = $(this).parents(".innerContainer");
        var innerType;   //文章模块类型  如editHeaderImg:大banner  editBigTitle:文章大标题  editsubTitle:文章小标题和作者
        innerType = $(this).attr("innerType");
        var editHtml = "";

        if (innerType == "editBigTitle") {
            editHtml = articleHeaderEditHtml;
            initCtrlHtml(editHtml);
            initRadioEvent(innerType, textAlign);
            if (title != null && title != undefined) {
                $("#bigTitle").val(title);
                $("#articleTiitle").html(title);
            }

            if (nick != null && nick != undefined) {
                $("#nick").val(nick);
                $("#author").html(nick);
            }

            if (textAlign != null && textAlign != undefined) {
                $("input[value='" + textAlign + "']").attr("checked", "checked");
            }

            $("#bigTitle").keyup(function () {
                $("#articleTiitle").html($("#bigTitle").val());
                hasTitle=true;
                title=$("#bigTitle").val();
            })

            $("#nick").keyup(function () {
                $("#author").html($("#nick").val());
                hasNick=true;
                nick=$("#nick").val();
            })

            $("input[value='left']").click(function () {
                textAlign = "left";
            })

            $("input[value='center']").click(function () {
                textAlign = "center";
            })

        }
        else if (innerType == "editsubTitle") {
            editHtml = abstractEditHtml;
            initCtrlHtml(editHtml);
            initRadioEvent(innerType, fontColor);
            if (subTitle != null && subTitle != undefined) {
                $("#subTitle").val(subTitle);
                $(".abstractText").html(subTitle);
            }

            if (fontColor != null && fontColor != undefined) {
                $("input[value='" + fontColor + "']").attr("checked", "checked");
            }

            $("#subTitle").keyup(function () {
                $(".abstractText").html($("#subTitle").val());
                hasSubTitle=true;
                subTitle=$("#subTitle").val();
            })

            $("input[value='orange']").click(function () {
                fontColor = "orange";
            })

            $("input[value='green']").click(function () {
                fontColor = "green";
            })

        }
        else if(innerType=="mainImg"){
        	editHtml = mainImgEditHtml;
            initCtrlHtml(editHtml);
            $(".upFile ").attr("index",index);
            
            $(".mainImgDesc").keyup(function(){
            	list[index].desc = $(this).val();
            })
        }
        else if (innerType == "paa") {
            editHtml = paragarphEditHtml;
            initCtrlHtml(editHtml);

            if (list[index].desc != null && list[index].desc != undefined) {
                $(".paragraphEdit").val(list[index].desc);
            }

            $(".paragraphEdit").keyup(function () {
                _this.parents(".paragraph").find(".paragraphText").html($(this).val());
                list[index].desc = $(this).val();
                list[index].enable=1;
            })
        }
        else if (innerType == "smallTitle") {
            editHtml = smallTitleEditHtml;
            initCtrlHtml(editHtml);

            if (list[index].desc != null && list[index].desc != undefined) {
                $(".smallTitleInput").val(list[index].desc);
            }

            $(".smallTitleInput").keyup(function () {
                _this.parents(".smallTitle").find(".smallTitleText").html($(this).val());
                list[index].desc = $(this).val();
                list[index].enable=1;
            })

        }
        else if (innerType == "goood") {
            var checkValue;
            editHtml = goodsEditHtml;
            initCtrlHtml(editHtml);

            if (list[index].tpl != null && list[index].tpl != undefined) {
            	_thisParent.find(".goodsIdText").html(list[index].bizId);
            	
                if(parseInt(list[index].tpl)==1){
                    checkValue="goodsTplOne";
                    $(".goodsIntro").css("display", "none");                    
                    _thisParent.find(".goodsintrotitle").css("display", "none");
                    _thisParent.find(".goodsintroText").css("display", "none");
                }
                else{
                    checkValue="goodsTplTwo";
                    $(".goodsIntro").css("display", "block");
                    _thisParent.find(".goodsintrotitle").css("display", "block");
                    _thisParent.find(".goodsintroText").css("display", "block");
                    if(list[index].desc != null && list[index].desc != undefined&&list[index].desc!=""){
                        $(".goodsIntroInput").val(list[index].desc);
                        _thisParent.find(".goodsintroText").html(list[index].desc);
                    }
                }
            }
            else{
                checkValue="goodsTplOne";
                list[index].tpl=1;
            }
            initRadioEvent(checkValue);

            if(list[index].bizId != null && list[index].bizId != undefined){
                $(".goodsInput").val(list[index].bizId);
            }

            $("input[value='goodsTplOne']").click(function () {
                $(".goodsIntro").css("display", "none");
                _thisParent.find(".goodsintrotitle").css("display", "none");
                _thisParent.find(".goodsintroText").css("display", "none");
                list[index].tpl = 1;
            })

            $("input[value='goodsTplTwo']").click(function () {
                $(".goodsIntro").css("display", "block");
                $(".goodsInput").val(list[index].desc);
                _thisParent.find(".goodsintrotitle").css("display", "block");
                _thisParent.find(".goodsintroText").css("display", "block");
                list[index].tpl = 2;
              
            })

            $(".goodsInput").keyup(function () {
                _thisParent.find(".goodsIdText").html($(".goodsInput").val());
                list[index].bizId = $(".goodsInput").val();
                list[index].enable=1;
            })
            
             $(".goodsIntroInput").keyup(function () {
                    _thisParent.find(".goodsintroText").html($(".goodsIntroInput").val());
                    list[index].desc = $(".goodsIntroInput").val();
                })

        }
        else if (innerType == "studio") {
            editHtml = studioEditHtml;
            initCtrlHtml(editHtml);
            if (list[index].bizId != null && list[index].bizId != undefined) {
                $(".studioInput").val(list[index].bizId);
            }

            $(".studioInput").keyup(function () {
                _this.parents(".studio").find(".studioText").html($(this).val());
                list[index].bizId = $(this).val();
                list[index].enable=1;
            })
        }
        else if (innerType == "selectedWork") {
            editHtml = selectedWorksEditHtml;
            initCtrlHtml(editHtml);

            if (list[index].bizList != null && list[index].bizList != undefined) {

            }
            
            $(".save").click(function(){
            	list[index].enable=1;
            	var bizList = [];
            	for(var k=0;k<$(".worksInput").length;k++){
            		bizList.push($(".worksInput").eq(k).val()); 
            	}
            	 list[index].bizList=bizList;
            	 alert("保存成功");
            })

            $(".selectedAdd").click(function () {
                var selectedLength = $(".worksInput").length + 1;
                var inputHtml = '<div class="item">作品 ' + selectedLength + 'id:<input class="worksInput" type="text" placeholder="作品 ' + selectedLength + 'id"></div>';
                $(".selectedBtn").before(inputHtml);
            })

        }
        else if (innerType == "articleEdit") {
            editHtml = articleRelatedEditHtml;
            initCtrlHtml(editHtml);

            $(".save").click(function(){
            	
            	list[index].enable=1;
            	var bizList = [];
            	for(var k=0;k<$(".articleInput").length;k++){
            		bizList.push($(".articleInput").eq(k).val()); 
            	}
            	 list[index].bizList=bizList;
            	 alert("保存成功");
            })

            $(".articleAdd").click(function () {

                var selectedLength = $(".articleInput").length + 1;
                var inputHtml = '<div class="item">文章 ' + selectedLength + 'id:<input class="articleInput" type="text" placeholder="文章 ' + selectedLength + 'id"></div>';
                $(".articleAdd").before(inputHtml);

            })

        }
        else if (innerType == "linkWord") {
            editHtml = linkEditHtml;
            initCtrlHtml(editHtml);

            if (list[index].title != null && list[index].title != undefined) {
                $(".linksEdit").val(list[index].title);
            }

            if (list[index].desc != null && list[index].desc != undefined) {
                $(".linksUrlEdit").val(list[index].desc);
            }

            $(".linksEdit").blur(function () {
                _this.parents(".links").find(".linkText").html(" 链接文本 " + $(this).val());
                list[index].title = $(this).val();
            })

            $(".linksUrlEdit").blur(function () {
                _this.parents(".links").find(".linkUrlText").html(" 链接地址: " + $(this).val());
                list[index].desc = $(this).val();
                list[index].enable=1;
            })

        }
        else if (innerType == "editVideo") {
            editHtml = videoEditHtml;
            initCtrlHtml(editHtml);
            
            $(".upFile ").attr("index",index);
        }
        else if (innerType == "auctionGood") {
            editHtml = auctionEditHtml;
            initCtrlHtml(editHtml);

            var checkValue;

            if (list[index].tpl != null && list[index].tpl != undefined) {

                if(parseInt(list[index].tpl)==1){
                    checkValue="auctionTplOne";
                    $(".auctionIntro").css("display", "none");
                    _thisParent.find(".auctionintrotitle").css("display", "none");
                    _thisParent.find(".auctionintroText").css("display", "none");
                }
                else{
                    checkValue="auctionTplTwo";
                    $(".auctionIntro").css("display", "block");
                    _thisParent.find(".auctionintrotitle").css("display", "block");
                    _thisParent.find(".auctionintroText").css("display", "block");
                    if(list[index].desc != null && list[index].desc != undefined&&list[index].desc!=""){
                        $(".auctionIntroInput").val(list[index].desc);
                        _thisParent.find(".auctionintroText").html(list[index].desc);
                    }
                }
            }
            else{
                checkValue="auctionTplOne";
                list[index].tpl=1;
            }
            initRadioEvent(checkValue);

            if(list[index].bizId != null && list[index].bizId != undefined){
                $(".auctionInput").val(list[index].bizId);
            }

            $("input[value='auctionTplOne']").click(function () {
                $(".auctionIntro").css("display", "none");
                _thisParent.find(".auctionintrotitle").css("display", "none");
                _thisParent.find(".auctionintroText").css("display", "none");
                list[index].tpl = 1;
            })

            $("input[value='auctionTplTwo']").click(function () {
                $(".auctionIntro").css("display", "block");
                _thisParent.find(".auctionintrotitle").css("display", "block");
                _thisParent.find(".auctionintroText").css("display", "block");
                list[index].tpl = 2;

                $(".auctionIntroInput").keyup(function () {
                    _thisParent.find(".auctionintroText").html($(".auctionIntroInput").val());
                    list[index].desc = $(".auctionIntroInput").val();
                })
            })

            $(".auctionInput").keyup(function () {
                _thisParent.find(".auctionIdText").html($(".auctionInput").val());
                list[index].bizId = $(".auctionInput").val();
                list[index].enable=1;
            })

        }

    });

    //加载右边编辑框
    function initCtrlHtml(editHtml) {
        $(".diy-cirl1").css("display", "block");
        $(".diy-cirl1").html(editHtml);
    }

    //对应的单选框选择
    function initRadioEvent(checkValue) {
        $("input[value='" + checkValue + "']").attr("checked", "checked");
    }

    //保存数据
    $(".saveData").click(function () {
    	  isSaveClick=true;
    	  isPreviewClick=false;
          uploadData();
    })
    
    //预览
    $(".preview").click(function () {
    	 isPreviewClick=true;
    	 isSaveClick=false;
  
    	 uploadData();
    	 setTimeout(function(){
    		 window.open("http://m2.xunbaozl.com/article.html?topicId="+articleId+"&isOnLine=0");    
    	 },1000); 
    })

    //验证数据
   function checkForm() {
    	
    	 while(true){
    		 if (hasHeaderImg == false) {
    				alert("请上传首图");
    				return false;
    			} else if (hasTitle == false) {
    				alert("请填写文章标题");
    				return false;
    			} else if (hasNick = false) {
    				alert("请填写作者");
    				return false;
    			} else if (hasSubTitle == false) {
    				alert("请填写摘要");
    				return false;
    			}

    			for (var i = 0; i < list.length; i++) {
    				if (list[i].enable != 1) {
    					alert(list[i].warnMsg);
    					return false;
    				}
    			}
           break;

        	}
    	 
        return true;
    }

    //上传数据
    function uploadData() {
    	var listData=[];
        if (checkForm()==true) {
            if (list.length > 0) {
                for(var i=0;i<list.length;i++){
                   var listItem={};
                   listItem.type=list[i].type;
                   listItem.postIndex=i;
                   if(list[i].type==0||list[i].type==6){
                	   listItem.desc=list[i].desc;
                   }
                   else if(list[i].type==1){
                	   listItem.imgKey=list[i].imgKey;
                	   listItem.cropHeight=list[i].cropHeight;
                	   listItem.cropWidth=list[i].cropWidth;
                	   listItem.desc=list[i].desc;
                   }
                   else if(list[i].type==2){
                	   listItem.video=list[i].video;
                	   listItem.videoImage=list[i].videoImage;
                	   listItem.cropHeight=list[i].cropHeight;
                	   listItem.cropWidth=list[i].cropWidth;
                   }
                   else if(list[i].type==3||list[i].type==8){
                	   listItem.bizId=list[i].bizId;
                	   listItem.tpl=list[i].tpl;
                	   listItem.desc=list[i].desc;
                   }
                   else if(list[i].type==7){
                	   listItem.bizId=list[i].bizId;
                   }
                   else if(list[i].type==4||list[i].type==5){
                	   var listObj=[];
                	   for(var j=0;j<list[i].bizList.length;j++){
                		   var listObjItem={};
                		   listObjItem.bizId=list[i].bizList[j];
                    	   listObj.push(listObjItem);
                	   }
                	  
                	   listItem.bizList=JSON.stringify(listObj);
                   }
                   else if(list[i].type==9){
                	   listItem.title=list[i].title;
                	   listItem.desc=list[i].desc;
                   }
                   listData.push(listItem);
                }
                articleData = JSON.stringify(listData);
            }
            
            title = $("#articleTiitle").html();
            nick = $("#author").html();
            subTitle = $(".abstractText").html();
            headerImgUrl = $("#headerImg").find("img").attr("src");

            //alert(articleData);

            var timeNow = new Date().pattern("yyyy-MM-dd HH:mm:ss");//获取当前编辑时间

            //return;
            //flag:0 新建  flag:1 更新
            var params = {};
            if (topicId != null && topicId != undefined && topicId != "") {
                params = {
                    "topicId": topicId,
                    "flag": 1,
                    "cropWidth": cropWidth,
                    "cropHeight": cropHeight,
                    "title": title,
                    "nick": nick,
                    "subTitle": subTitle,
                    "textAlign": textAlign,
                    "fontColor": fontColor,
                    "url": headerImgUrl,
                    "createdAt": timeNow,
                    "articleData": articleData
                };
            }
            else {
                params = {
                    "flag": 0,
                    "title": title,
                    "cropWidth": cropWidth,
                    "cropHeight": cropHeight,
                    "nick": nick,
                    "subTitle": subTitle,
                    "textAlign": textAlign,
                    "fontColor": fontColor,
                    "url": headerImgUrl,
                    "createdAt": timeNow,
                    "articleData": articleData
                };
            }

            //return;
            utils.jsonp({
                url: "http://manage.xunbaozl.com/cms/post/article/addArticle",
                type: "POST",
                params: params,
                success: function (resul) {
                   if(resul.data==true){
                   	 if(isSaveClick==true){
                   	 	alert("编辑成功");  
                   	 	}              	                 	   
                	   articleId=resul.topicId;
                   }
                   else{
                	   alert("编辑失败");
                   }
                }
            })
        }       
    }
})